package com.ninetaildemonfox.zdl.socialproject.mvp.presenter;

import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.RadioStationContract;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/8/10
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class RadioStationPresenter {
    private RadioStationContract contract;
    private AppCompatActivity me;

    public RadioStationPresenter(RadioStationContract contract, AppCompatActivity me) {
        this.contract = contract;
        this.me = me;
    }

    public void getOrderList(final String token, final String page, final String type, final String city) {
        if (TextUtils.isEmpty(city)) {
            Model.getInstance().getUserApi().getOrderList(token, page, type, "", "1")
                    .enqueue(new Callback<JsonObject>() {
                        @Override
                        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                            Map<String, String> map = Constants.codeGet(me, response);
                            if (map == null) {
                                return;
                            }
                            ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                            contract.RadioSuccess(list);
                        }

                        @Override
                        public void onFailure(Call<JsonObject> call, Throwable t) {
                            ToastUtil.show("您的网络不佳，请稍候重试");
                        }
                    });
            return;
        }
        Model.getInstance().getUserApi().getQueryIdbyName(city).enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                Map<String, String> map = Constants.codeGet(me, response);
                if (map == null) {
                    return;
                }
                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                if (data == null) {
                    return;
                }
                Model.getInstance().getUserApi().getOrderList(token, page, type, data.get("id"), "1")
                        .enqueue(new Callback<JsonObject>() {
                            @Override
                            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                                Map<String, String> map = Constants.codeGet(me, response);
                                if (map == null) {
                                    return;
                                }
                                ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                                contract.RadioSuccess(list);
                            }

                            @Override
                            public void onFailure(Call<JsonObject> call, Throwable t) {

                            }
                        });

            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {

            }
        });
    }

    /**
     * 点赞
     */
    public void addOrderGood(String token, String order_id, final int item) {
        Model.getInstance().getUserApi().addOrderGood(token, order_id)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        ToastUtil.show(map.get("message"));
                        contract.addOrderGood(item,1);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    /**
     * 报名
     */
    public void addEnlist(String token, String order_id, final int item, File file) {

        if (file == null) {
            ToastUtil.show("文件不能为空");
            return;
        }
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("application/otcet-stream"), file);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("img", file.getName(), requestFile);

        Model.getInstance().getUserApi().addEnlist(token, order_id, body)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        ToastUtil.show(map.get("message"));
                        contract.addEnlist(item);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    /**
     * 取消报名
     */
    public void endOrder(String token, String order_id, final int item) {
        HttpRequest.POST(me, "Order/endOrder"
                , new Parameter()
                        .add("token", token)
                        .add("order_id", order_id)
                , new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        if (error != null) {
                            return;
                        }
                        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                        if (map.get("code").equals("1")) {
                            contract.addOrderGood(item,2);
                            return;
                        }
                        ToastUtil.show(map.get("message"));

                    }
                });

    }


}